#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QString>
#include<QGraphicsScene>
#include<QImage>
#include<QPushButton>
#include<QStringList>
#include<QStringListModel>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{

    ui->setupUi(this);
    QImage *image=new QImage();
    image->load("/home/square/Downloads/2c4a2c2fc38ca178c130a4605dc46120.jpg");
    double scale1 = ui->graphicsView->width()/(double)image->width();
    double scale2 = ui->graphicsView->height()/(double)image->height();
    double scale = (scale1>scale2)?scale2:scale1;
    QImage new_image = image->scaled(int(image->width()*scale)-1,int(image->height()*scale)-1);
    QGraphicsScene *scene = new QGraphicsScene;
    scene->addPixmap(QPixmap::fromImage(new_image));
    ui->graphicsView->setScene(scene);
    ui->graphicsView->show();



    QNetworkAccessManager* manager = new QNetworkAccessManager(this);
    QNetworkRequest request;
    request.setUrl(QUrl("http://127.0.0.1/getall?name=test"));
    QNetworkReply* reply = manager->get(request);
    connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinish(QNetworkReply*)));

    strList = new QStringList();
    strList->append("1");
    strList->append("22");
    QStringListModel *m_model=new QStringListModel(*strList);
    ui->listView->setModel(m_model);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::replyFinish(QNetworkReply* reply)
{

    QString rp = reply->readAll();
    //strList = new QStringList();

    int i=0;
    QString buff;
    bool isbegin=false;
    while(++i != rp.size()){
        if(rp[i] == "[" || rp[i] == "," || rp[i] == ","){
            continue;
        }
        else if(rp[i] == "\""){
            if(isbegin == false){
                isbegin = true;
            }
            else{
                strList->append(buff);
                isbegin=false;
                buff = QString();
            }
        }else {
            buff.append(rp[i]);
        }
    }

    QStringListModel *m_model=new QStringListModel(*strList);
    ui->listView->setModel(m_model);

//    if(rp == "success"){
//        MainWindow *m = new MainWindow();
//        m->show();
//        this->close();
//    }
}
